package com.hotkid.ceo.jetpack.myjetpackmvvm.util

import android.animation.ObjectAnimator
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.ImageView
import com.hotkid.ceo.jetpack.myjetpackmvvm.R

/**
 * ClassName:ViewExt
 * Package:com.hotkid.ceo.jetpack.myjetpackmvvm.util
 * Description:
 * @date:2021/3/24 10:15 AM
 * @author:774169396@qq.com
 */
/**
 * imageView旋转动画180度
 */
fun startImageRotate(imageView: ImageView, toggle: Boolean) {
    val tarRotate = if (toggle) 0F else 180F
    imageView.apply {
        ObjectAnimator.ofFloat(this, "rotation", rotation, tarRotate).let {
            it.duration = 300
            it.start()
        }
    }
}


/**
 * view展开收起动画
 */

fun View.topAnimation(show: Boolean) {
    //要显示或不显示
    if ((show && visibility == View.GONE) || (!show && visibility == View.VISIBLE)) {
        visibility = if (show) View.VISIBLE else View.GONE
        animation = AnimationUtils.loadAnimation(context, if (show) R.anim.top_in else R.anim.top_out)
    }
}

/**
 * Veiw淡入淡出动画
 */

fun View.alphaAnimation(show: Boolean) {
    if ((show && visibility == View.GONE) || (show && visibility == View.VISIBLE)) {
        visibility = if (show) View.VISIBLE else View.GONE
        animation = AnimationUtils.loadAnimation(context, if (show) R.anim.alpha_in else R.anim.alpha_out)
    }
}

/**
 * 设置View 背景
 */

fun View.setRoundRectBg(color: Int = Color.WHITE, radius: Float = 10F) {
    background = GradientDrawable().apply {
        setColor(color)
        cornerRadius = radius
    }
}


/**
 * 防止重复点击
 */

var lastClickTime=0L
fun View.setOnclickNoRepeat(interal:Long=500,onClick:(View)->Unit){
    this.setOnClickListener {
        val currentTime=System.currentTimeMillis()
        if (lastClickTime!=0L&&(currentTime- lastClickTime<interal)){
            return@setOnClickListener
        }

        lastClickTime=currentTime
        onClick.invoke(it)
    }
}
